﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Constructor
{
    public partial class SlotConstructor : UserControl
    {
        private Frame _frame;
        public SlotConstructor()
        {
            InitializeComponent();
        }
        public SlotConstructor(Frame frame)
        {
            InitializeComponent();
            _frame = frame;
        }
        public Frame Frame
        {
            get { return _frame; }
        }
        public void Init(Frame frame)
        {
            _frame = frame;
            btn_add.Enabled = btn_delete.Enabled = !(frame.Name == "Клетка" || frame.Name == "Объект");
            Refresh();
        }
        public void Init(Frame frame, Slot s)
        {
            _frame = frame;
            btn_add.Enabled = btn_delete.Enabled = !(frame.Name == "Клетка" || frame.Name == "Объект");
            Refresh();
            foreach (ListViewItem item in lv_slots.Items)
                if ((item.Tag as Slot).FullName == s.FullName)
                    item.Selected = true;

        }
        public void  Refresh()
        {
            lv_slots.Items.Clear();
            
            foreach (Slot slot in _frame.OnlyOwnSlots)
            {
                ListViewItem item = new ListViewItem(new string[] { "", "", "", "" });
                item.Tag = slot;
                slot.Item = item;
                slot.RepaintItem();
                lv_slots.Items.Add(item);
                if (slot.Frame.Name == _frame.Name)
                    slot.ChangeType += new EventHandler(slot_ChangeType);                
            }
            foreach (Slot slot in _frame.ParentSlots)
            {
                ListViewItem item = new ListViewItem(new string[] { "", "", "", "" });
                item.BackColor = System.Drawing.Color.Gray;
                Slot s = slot.Frame.SearchSlot(slot.Name) ?? slot;
                if (s != null && !External.CompareSlots(slot, s))
                {
                    slot.Frame.Slots.Remove(s);
                    s = slot;
                }
                item.Tag = s;
                s.isOwn = false;
                s.Item = item;
                s.RepaintItem();
                lv_slots.Items.Add(item);
                if (s.Frame.Name == _frame.Name)
                    s.ChangeType += new EventHandler(slot_ChangeType);
            }
            if (lv_slots.Items.Count > 0)
                lv_slots.SelectedIndices.Add(0);
        }

        void slot_ChangeType(object sender, EventArgs e)
        {
            
        }
        public SlotConstructor(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            Slot slot = new Slot(_frame);
            _frame.Slots.Add(slot);
            ListViewItem item = new ListViewItem(new string[] { "", "", "", "" });
            item.Tag = slot;
            slot.Item = item;
            slot.RepaintItem();
            lv_slots.Items.Add(item);
            propertiesSlot1.Init(slot);
            Refresh();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (lv_slots.SelectedIndices.Count > 0)
            {
                _frame.Slots.Remove(lv_slots.SelectedItems[0].Tag as Slot);
                lv_slots.Items.Remove(lv_slots.SelectedItems[0]);
                propertiesSlot1.Hide();
            }
        }

        private void lb_slots_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv_slots.SelectedItems.Count > 0)
                propertiesSlot1.Init(lv_slots.SelectedItems[0].Tag as Slot);
        }

        private void btn_up_Click(object sender, EventArgs e)
        {
            if (lv_slots.SelectedIndices.Count > 0)
            {
                int curr = lv_slots.SelectedIndices[0];
                if (curr == 0) return;
                _frame.MoveSlot(lv_slots.Items[curr].Tag as Slot, -1);
                
                var item = lv_slots.Items[curr];
                lv_slots.Items.RemoveAt(curr);
                lv_slots.Items.Insert(curr - 1, item);
            }
        }

        private void btn_down_Click(object sender, EventArgs e)
        {
            if (lv_slots.SelectedIndices.Count > 0)
            {
                int curr = lv_slots.SelectedIndices[0];
                if (curr == lv_slots.Items.Count-1) return;
                if (_frame.Slots.IndexOf(lv_slots.Items[curr].Tag as Slot) == _frame.Slots.Count-1) return;
                _frame.MoveSlot(lv_slots.Items[curr].Tag as Slot, + 1);                
                var item = lv_slots.Items[curr];
                lv_slots.Items.RemoveAt(curr);
                lv_slots.Items.Insert(curr + 1, item);
            }
        }
    }
}
